"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.
"""

"""
Case Type   : 资源池化
Case Name   : 资源池化场景下,主备机针对物化视图的操作
Create At   : 2025-03-03
Owner       : zhongchengzhi
Description :
    1、查询集群状态
    2、主机创建基表并插入数据
    3、主机创建物化视图
    4、主机修改物化视图的基表数据
    5、主机刷新物化视图
    6、主机查询物化视图
    7、备机查询物化视图
    8、清理数据、恢复集群状态

Expect      :
    1、查询结果正常
    2、创建、插入数据成功
    3、创建物化视图成功
    4、修改数据成功
    5、刷新物化视图成功
    6、查询物化视图结果正常
    7、查询物化视图结果正常
    8、清理数据、恢复集群状态成功
History     :
    1、2025-03-03 创建
    2、2025-07-12 改造用例为1p1s环境可执行
"""
import os
import unittest
import time

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_ROOTSH = CommonSH('PrimaryRoot')
Primary_SH = CommonSH('PrimaryDbUser')
Standby1_SH = CommonSH('Standby1DbUser')

@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.s1_comsh = CommonSH('Standby1DbUser')

    def test_shared_storage(self):
        text = '-----step1:查询数据库集群状态-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cvipd')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:创建表1 并插入数据;expect:成功-----'
        self.log.info(text)
        sql = f'''
                drop table if exists t1 cascade;
                create table t1 (c_int int, c_char char(1));
                insert into t1 values (1, 'a');
                insert into t1 values (2, 'b');
                '''
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('CREATE TABLE\nINSERT 0 1\nINSERT 0 1', status, '执行失败' + text)

        text = '-----step3:主机创建物化视图;expect:创建物化视图成功-----'
        self.log.info(text)
        sql = f"CREATE MATERIALIZED VIEW mv AS select count(*) from t1;"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('CREATE MATERIALIZED VIEW', status, '执行失败' + text)

        text = '-----step4:主机修改物化视图基表的数据;expect:成功-----'
        self.log.info(text)
        sql = f"insert into t1 values (3, 'c');"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('INSERT 0 1', status, '执行失败' + text)

        text = '-----step5:主机刷新物化视图 expect:刷新成功-----'
        self.log.info(text)
        sql = f"REFRESH MATERIALIZED VIEW mv;"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('REFRESH MATERIALIZED VIEW', status, '执行失败' + text)

        text = '-----step6:备机查询物化视图结果;expect:查询物化视图结果刷新-----'
        self.log.info(text)
        sql = f"select * from mv;"
        self.log.info(sql)
        status = Standby1_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('(1 row)', status, '执行失败' + text)

        text = '-----step7:主机查询物化视图结果;expect:查询物化视图结果刷新-----'
        self.log.info(text)
        sql = f"select * from mv;"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('(1 row)', status, '执行失败' + text)

        text = '-----step8:清理环境;expect:成功-----'
        self.log.info(text)
        sql = f'''
                drop MATERIALIZED VIEW mv;
                drop table t1;
                '''
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('DROP MATERIALIZED VIEW\nDROP TABLE', status, '执行失败' + text)

    def tearDown(self):
        text = '-----step9:恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn('cm_ctl: stop cluster successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='start')
        self.log.info(status)
        self.assertIn('cm_ctl: start cluster successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(status)
        self.assertIn(self.constant.cm_switchover_success_msg, status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cvipd')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')